import { cnode } from '../utils/services'

export default {
  namespace: 'count',// 命名空间 - 不是vue的namespaced(属性值为布尔值)
  state: {
    n: 1
  },// 初始值
  subscriptions: {// 监听-history是路由
    setup({ dispatch, history }) {  // eslint-disable-line
    },
  },
  effects: {// 异步saga - saga插件使用generator函数
    *fetch({ payload }, { call, put }) {  // eslint-disable-line
      yield put({ type: 'save' });
    },
    *cnodeFunc({ payload }, { call, put }) {
      let res = yield call(cnode, payload)
      console.log(res)
      yield put({type: 'inc', payload: res.data.length})
    }
  },
  reducers: {// 同步
    save(state, action) {
      // action = {type: "example/inc", payload: 2}
      // {type: "example/inc", @@redux-saga/SAGA_ACTION: true} - 经异步put执行的reducers会带上这个属性
      return { ...state, ...action.payload };
    },
    inc(state, action) {
      return { ...state, n: state.n + action.payload }
    }
  }
};